package com.wpc.smspringmvc.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wpc.smspringmvc.model.Page;
import com.wpc.smspringmvc.model.User;
import com.wpc.smspringmvc.service.UserService;

/**
 * 用户控制层
 * 
 * @author wpc
 *
 */
@Controller
public class UserController{

	@Autowired
	private UserService userService;
	
	/**
	 * 添加用户
	 * 
	 * @return
	 */
	@RequestMapping(value="/user",method=RequestMethod.POST)
	public String addUsers(@RequestBody User user) throws Exception{
		userService.addUser(user);
		return "user_list";
	}
	
	/**
	 * 编辑用户
	 * 
	 * @return
	 */
	@RequestMapping(value="/user",method=RequestMethod.PUT)
	public String editorUsers(@RequestBody User user) throws Exception{
		userService.editorUser(user);
		return "user_list";
	}
	
	/**
	 * 根据ID查询用户
	 * 
	 * @return
	 */
	@RequestMapping(value="/user/id",method=RequestMethod.GET)
	public String getUserById(@RequestBody User user) throws Exception{
		
		user = userService.getUserById(user);
        //ServletActionContext.getRequest().setAttribute("user", user);
		return "user_list";
	}
	
	/**
	 * 删除用户
	 * 
	 * @return
	 */
	@RequestMapping(value="/user",method=RequestMethod.DELETE)
	public String deleteUser(@RequestBody User user) throws Exception{
		userService.deleteUser(user);
		return "user_list";
	}
	
	/**
	 * 查询用户
	 * 
	 * @return
	 */
	@RequestMapping(value="/user/getusers",method=RequestMethod.GET)
	public Page getUsers(@RequestBody User user, Page page) throws Exception{
		System.out.println(user.getName()+"  " +page.getPageIndex());
		Map<String,Object> condition=new HashMap<String,Object>();
		condition.put("pageSize", page.getPageSize());
		condition.put("pageIndex", page.getPageIndex());
		condition.put("user", user);
		
		return userService.getUsers(condition);
	}
	
}
